<template>
    <div class="wrapper">
        <div class="topWrapper">
            <img class="bg" style="min-height: 170px" src="@/assets/images/bg.png" alt="" />
            <div class="content">
                <div class="top">总收益 (元)</div>
                <div class="center">
                    <div class="left">
                        ￥
                        <span>{{ sixProfit.total }}</span>
                    </div>
                    <div class="right" v-if="bountyShow === Y">服务收益 {{ sixProfit.bounty }}</div>
                </div>
                <div class="bottom">
                    <div class="left">交易收益 {{ sixProfit.profit }}</div>
                    <div class="right">返现收益 {{ sixProfit.activate }}</div>
                </div>
            </div>
        </div>
        <div class="bottomWrapper">
            <div class="content">
                <div class="title">
                    <div class="left">
                        <img src="@/assets/index/icon1.png" alt="" />
                        <span>收益趋势</span>
                    </div>
                    <div class="right" @click="navTo('/monEarnings')">
                        <span>查看月收益</span>
                        <img src="@/assets/chevron-down@2x.png" alt="" />
                    </div>
                </div>
                <div class="echarts">
                    <div class="head">
                        <van-tabs v-model:active="active" @click="getFindProActDayByTime" type="card" shrink color="#66d6ff">
                            <van-tab title="近7天" />
                            <van-tab title="近30天" />
                        </van-tabs>
                    </div>
                    <div id="myChart" :style="{ width: '100%', height: '250px' }"></div>
                </div>
            </div>
        </div>
        <div class="bottomWrapper">
            <div class="content">
                <div class="title">
                    <div class="left">
                        <img src="@/assets/index/icon2.png" alt="" />
                        <span>收益明细</span>
                    </div>
                    <div class="right" @click="navTo('/earningDetail')">
                        <span>查看明细</span>
                        <img src="@/assets/chevron-down@2x.png" alt="" />
                    </div>
                </div>
                <div class="myTable">
                    <table width="100%">
                        <tr>
                            <th>日期</th>
                            <th>总收益</th>
                            
                            <th>交易</th>
                            <th>返现</th>
                            <th v-if="bountyShow === 'Y'">服务收益</th>
                        </tr>
                        <tr v-for="item in list" :key="item.totalDate">
                            <td>{{ item.totalDate }}</td>
                            <td>{{ item.totalAmount }}</td>
                            
                            <td>{{ item.profitAmount }}</td>
                            <td>{{ item.backAmount }}</td>
                            <td v-if="bountyShow === 'Y'">{{ item.profitBountyAmount }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { findProActDayByTime, totalStatisticSummary, findCostFeeByAgentNo } from '@/api/agent'
import * as echarts from 'echarts'
import { getDay } from '../../utils/utils'
export default {
    name: 'earnings',
    data() {
        return {
            active: 0,
            list: [],
            bountyShow: 'N',
            echartsDay: 7,
            sixProfit: {}
        }
    },
    mounted() {
        findCostFeeByAgentNo().then(res => {
            this.bountyShow = res.object.bountyShow
        })
        //获取近六个月的分润
        totalStatisticSummary().then(res => {
            const { code, object } = res
            if (code == 200) {
                this.getFindProActDayByTime()
                this.sixProfit = object
            }
        })
    },
    methods: {
        navTo(url) {
            this.$router.push(url)
        },
        getFindProActDayByTime() {
            this.active ? (this.echartsDay = 30) : (this.echartsDay = 7)
            findProActDayByTime({
                timeStart: getDay(this.echartsDay - 1),
                timeEnd: getDay(0)
            }).then(res => {
                let dateList = []
                let valueList = []
                this.list = res.object || []
                res.object.forEach(item => {
                    dateList.push(item.totalDate)
                    valueList.push(item.totalAmount)
                })
                this.showorders(dateList, valueList)
            })
        },
        showorders(dateList, valueList) {
            // 基于准备好的dom，初始化echarts实例
            document.getElementById('myChart').setAttribute('_echarts_instance_', '')

            var chartDom = document.getElementById('myChart')
            if (this.myChart != null && this.myChart != '' && this.myChart != undefined) {
                this.myChart.dispose() //销毁
            }
            this.myChart = echarts.init(chartDom)
            // 指定图表的配置项和数据
            var option
            option = {
                axisLine: {
                    lineStyle: {
                        color: '#ffffff'
                    }
                },
                xAxis: {
                    type: 'category',
                    data: dateList
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: valueList,
                        type: 'line',
                        smooth: true,
                        itemStyle: {
                            normal: {
                                color: '#66d6ff', //改变折线点的颜色
                                lineStyle: {
                                    color: '#66d6ff' //改变折线颜色
                                }
                            }
                        }
                    }
                ]
            }
            option && this.myChart.setOption(option)
        }
    }
}
</script>
bg

<style scoped lang="scss">
.wrapper {
    height: 100%;
    display: flex;
    flex-direction: column;

    .topWrapper {
        position: relative;
        font-size: 0;
        padding: 10px;
        box-sizing: border-box;

        .bg {
            width: 100%;
        }

        .content {
            position: absolute;
            top: 25px;
            left: 0;
            height: 100%;
            width: 100%;
            padding: 16px 40px;
            box-sizing: border-box;
            color: #ffffff;
            line-height: 35px;

            .top {
                font-size: 16px;
            }

            .center {
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-size: 14px;

                span {
                    font-size: 26px;
                }
            }

            .bottom {
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-size: 14px;
            }
        }
    }

    .bottomWrapper {
        flex: 1;
        margin-top: 10px;
        width: 100%;
        background-color: #ffffff;

        .content {
            border-radius: 10px 10px 0px 0px;
            background-color: #ffffff;
            height: 100%;
            padding: 0 10px;

            .title {
                display: flex;
                align-items: center;
                justify-content: space-between;
                font-size: 0;
                padding: 10px 0;

                border-bottom: 1px solid #e4e4e4;

                .left,
                .right {
                    display: flex;
                    align-items: center;
                }

                .left {
                    img {
                        margin-right: 10px;
                    }
                }

                span {
                    font-size: 14px;
                }

                img {
                    width: 14px;
                }
            }

            .echarts {
                padding-top: 10px;

                .head {
                }
            }
        }
    }
}
</style>
<style lang="scss" scoped>
.van-tab {
    font-size: 12px;
}

table {
    text-align: center;
    margin-top: 10px;

    tr {
        border-bottom: 1px solid rgb(245, 245, 245);
    }

    td,
    th {
        height: 30px;
        width: auto;
    }
}
</style>
